package com.ybu.handler;

import com.ybu.common.Constants;
import com.ybu.common.JwtUtils;
import com.ybu.common.ServiceCode;
import com.ybu.common.ServiceException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TokenInterceptorHandler implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从请求头中获取token
        String token = request.getHeader(Constants.TOKEN_HEADER);
        if(token==null || token == ""){
            throw new ServiceException(ServiceCode.ERR_JWT_LOGOUT,"token不能为空");
        }
        //校验
        JwtUtils.verifyToken(token);
        //说明token合法的
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
